package com.ly1.aicosplay.repository;

import com.ly1.aicosplay.domain.po.Character;
import com.ly1.aicosplay.config.CharacterFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * @author Ly
 */
@Repository
public class CharacterRepository {
    
    @Autowired
    private CharacterFactory characterFactory;
    
    @Autowired
    private CharacterMapper characterMapper; ;
    
    /**
     * 根据关键词搜索角色
     * @param keyword 搜索关键词
     * @return 匹配的角色列表
     */
    public List<Character> searchCharacters(String keyword) {
        return characterMapper.searchByName(keyword);
    }
    
    /**
     * 根据ID获取角色
     * @param id 角色ID
     * @return 角色对象
     */
    public Character getCharacterById(String id) {
        return characterFactory.getCharacterById(id);
    }
    
    /**
     * 获取所有角色
     * @return 所有角色列表
     */
    public List<Character> getAllCharacters() {
        return characterMapper.findAll();
    }
    
    /**
     * 保存角色
     * @param character 角色对象
     * @return 保存后的角色对象
     */
    public Character saveCharacter(Character character) {
        Character existingCharacter = characterMapper.findById(character.getId());
        if (existingCharacter != null) {
            characterMapper.update(character);
        } else {
            characterMapper.insert(character);
        }
        return character;
    }
}